<?php


class Usuario extends AppModel {
	
	public $name = 'Usuario';	
	
	public $validate = array(
        'username' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'A Usuario es requerido',
				'allowEmpty' => false
            ),
			'between' => array( 
				'rule' => array('between', 5, 15), 
				'required' => true, 
				'message' => 'El usuario debe estar entre 5 a 15 caracteres'
			),
			 'unique' => array(
				'rule'    => array('isUniqueUsername'),
				'message' => 'Este Usuario ya esta en uso'
			),
			'alphaNumericDashUnderscore' => array(
				'rule'    => array('alphaNumericDashUnderscore'),
				'message' => 'El Usuario solo puede ser letras y numeros'
			),
        ),
        'clave' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Se requiere una clave'
            ),
			'min_length' => array(
				'rule' => array('minLength', '6'),  
				'message' => 'La clave debe tener un minimo de 6 caracteres'
			)
        ),
		
		'password_confirm' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Porfavor confirmar su clave'
            ),
			 'equaltofield' => array(
				'rule' => array('equaltofield','clave'),
				'message' => 'Ambas contraseñas deben ser iguales.'
			)
        ),
		
		'email' => array(
			'required' => array(
				'rule' => array('email', true),    
				'message' => 'Porfavor, proporcionar un email.'    
			),
			 'unique' => array(
				'rule'    => array('isUniqueEmail'),
				'message' => 'Este email ya esta en uso',
			),
			'between' => array( 
				'rule' => array('between', 6, 30), 
				'message' => 'El usuario debe ser entre 6 a 30 caracteres'
			)
		),
    
		
		'password_update' => array(
			'min_length' => array(
				'rule' => array('minLength', '6'),   
				'message' => 'Clave sebe ser un minimo de 6 caracteres',
				'allowEmpty' => true,
				'required' => false
			)
        ),
		'password_confirm_update' => array(
			 'equaltofield' => array(
				'rule' => array('equaltofield','password_update'),
				'message' => 'ambas claves deben ser iguales.',
				'required' => false,
			)
        )

		
    );
	
	
	function isUniqueUsername($check) {

		$username = $this->find(
			'first',
			array(
				'fields' => array(
					'Usuario.id',
					'Usuario.username'
				),
				'conditions' => array(
					'Usuario.username' => $check['username']
				)
			)
		);

		if(!empty($username)){
			if($this->data[$this->alias]['id'] == $username['Usuario']['id']){
				return true; 
			}else{
				return false; 
			}
		}else{
			return true; 
		}
    }
	
	function isUniqueEmail($check) {

		$email = $this->find(
			'first',
			array(
				'fields' => array(
					'User.id'
				),
				'conditions' => array(
					'User.email' => $check['email']
				)
			)
		);

		if(!empty($email)){
			if($this->data[$this->alias]['id'] == $email['User']['id']){
				return true; 
			}else{
				return false; 
			}
		}else{
			return true; 
		}
    }
	
	public function alphaNumericDashUnderscore($check) {
        // $data array is passed using the form field name as the key
        // have to extract the value to make the function generic
        $value = array_values($check);
        $value = $value[0];

        return preg_match('/^[a-zA-Z0-9_ \-]*$/', $value);
    }
	
	public function equaltofield($check,$otherfield) 
    { 
        //get name of field 
        $fname = ''; 
        foreach ($check as $key => $value){ 
            $fname = $key; 
            break; 
        } 
        return $this->data[$this->name][$otherfield] === $this->data[$this->name][$fname]; 
    } 

	
	 public function beforeSave($options = array()) {
		// hash our password
		if (isset($this->data[$this->alias]['clave'])) {
			$this->data[$this->alias]['clave'] = AuthComponent::password($this->data[$this->alias]['clave']);
		}
		
		// if we get a new password, hash it
		if (isset($this->data[$this->alias]['password_update'])) {
			$this->data[$this->alias]['clave'] = AuthComponent::password($this->data[$this->alias]['password_update']);
		}
	
		// fallback to our parent
		
		return parent::beforeSave($options);
	}

}
?>